package socketmvc.core.processor.bean.optimizer.ret;

import com.alibaba.fastjson.JSONObject;
import socketmvc.core.context.IContext;
import socketmvc.core.packets.basic.IPacket;
import socketmvc.core.packets.basic.impl.JsonTextBody;
import socketmvc.core.packets.basic.impl.JsonSimplePacket;
import socketmvc.core.processor.bean.ProcessorMethodReturn;

public class DefRetSerializableObjectValResponseOptimizer extends DefAbstractRetOptimizer<Object> {

    private final String returnName;
    public DefRetSerializableObjectValResponseOptimizer(String returnName) {
        this.returnName = returnName;
    }

    @Override
    public IPacket doValue(IContext context, IPacket packet, Object returnVal, JsonTextBody responseTextBody) {
        JSONObject body = new JSONObject();
        body.put(returnName,returnVal);
        responseTextBody.setBody(body);
        return new JsonSimplePacket(responseTextBody);
    }
}
